package com.hjqjl.whdemo.feature.feature0.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjqjl.whdemo.R;
import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.databinding.ActivityMainBinding;
import com.hjqjl.whdemo.feature.feature0.ui.activity.custom.CustomControlActivity;
import com.hjqjl.whdemo.feature.feature0.ui.activity.list.ListSelectActivity;
import com.hjqjl.whdemo.feature.feature0.ui.activity.surfaceview.SurfaceViewActivity;
import com.hjqjl.whdemo.feature.feature0.ui.activity.tablayout.TabActivity;
import com.hjqjl.whdemo.feature.feature0.ui.activity.wakelock.WakeLockActivity;
import com.hjqjl.whdemo.utils.ActivityUtils;

public class MainActivity extends BaseActivity {

    public static void start(Context context) {
        Intent starter = new Intent(context, MainActivity.class);
        context.startActivity(starter);
    }

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        binding.tvVersionName.setText(AppUtils.getAppVersionName() + "--" + AppUtils.getAppVersionCode());
        binding.btnMainWelcome.setOnClickListener(v -> WelcomeActivity.start(pActivity));
        binding.btnSet.setOnClickListener(v -> SetActivity.start(pActivity));
        binding.btnMainNetwork.setOnClickListener(v -> NetWorkActivity.start(pActivity));
        binding.btnMainDb.setOnClickListener(v -> DBActivity.start(pActivity));
        binding.btnWorkManager.setOnClickListener(v -> WorkManagerActivity.start(pActivity));
        binding.btnMainScrollview.setOnClickListener(v -> ScrollViewActivity.start(pActivity));
        binding.btnMainSpinner.setOnClickListener(v -> SpinnerActivity.actionStart(pActivity));
        binding.btnMainList.setOnClickListener(v -> ListSelectActivity.start(pActivity));
        binding.btnMainCustomControl.setOnClickListener(v -> CustomControlActivity.actionStart(pActivity));
        binding.btnMainService.setOnClickListener(v -> ServiceActivity.actionStart(pActivity));
        binding.btnSeekBar.setOnClickListener(v -> SeekBarActivity.start(pActivity));
        binding.btnCustomActivity.setOnClickListener(v -> CustomActivity.start(pActivity));
        binding.btnTab.setOnClickListener(v -> TabActivity.start(pActivity));
        binding.btnPermission.setOnClickListener(v -> PermissionActivity.start(pActivity));
        binding.btnImageShow.setOnClickListener(v -> ImageActivity.start(pActivity));
        binding.btnHandler.setOnClickListener(v -> HandlerActivity.start(pActivity));
        binding.btnSurface.setOnClickListener(v -> SurfaceViewActivity.start(pActivity));
        binding.btnWakelock.setOnClickListener(v -> WakeLockActivity.start(pActivity));
    }

    private long mExitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            //两秒之内按返回键就会退出
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                ToastUtils.showShort("再按一次退出程序");
                mExitTime = System.currentTimeMillis();
            } else {
                ActivityUtils.getInstance().exit();
//                AppUtils.exitApp();// 退出时特别生硬
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}